<?php
/**
*
* @author kmklr72 (Kevin Murek) kmklr72@charter.net 
* @package umil
* @copyright (c) 2009 kmklr72 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('mods/chat_mod_install');

if (!file_exists($phpbb_root_path . 'umil/umil.' . $phpEx))
{
	trigger_error('Please download the latest UMIL (Unified MOD Install Library) from: <a href="http://www.phpbb.com/mods/umil/">phpBB.com/mods/umil</a>', E_USER_ERROR);
}

// We only allow a founder to install this MOD
if ($user->data['user_type'] != USER_FOUNDER)
{
	if ($user->data['user_id'] == ANONYMOUS)
	{
		login_box('', 'LOGIN');
	}

	trigger_error('NOT_AUTHORISED');
}

if (!class_exists('umil'))
{
	include($phpbb_root_path . 'umil/umil.' . $phpEx);
}

$umil = new umil(true);

$mod = array(
	'name'		=> 'Demon Chat',
	'version'	=> '0.0.1',
	'config'	=> 'chat_version',
	'enable'	=> 'chat_enable',
);

if (confirm_box(true))
{
	// Install the base 0.0.2 version
	if (!$umil->config_exists($mod['config']))
	{
		// Lets add a config setting for enabling/disabling the MOD and set it to true
		$umil->config_add($mod['enable'], true);

		// We must handle the version number ourselves.
		$umil->config_add($mod['config'], '0.0.2');
	}

	switch ($config['chat_version'])
	{
		case '0.0.1':
			$umil->permission_add('u_chat_mod', true);
			$umil->permission_add('u_chat_ban', true);

			$umil->module_add(array(
				array('acp', 'ACP_CAT_DOT_MODS', 'ACP_CAT_CHAT_MOD'),
			));

			$umil->module_add(array(
				array('acp', 'ACP_CAT_CHAT_MOD', 
					array('module_basename'	=> 'chat'),
				),
			));

			$umil->table_add('phpbb_chat_config', array(
				'COLUMNS'	=> array(
					'config_name'		=> array('VCHAR', ''),
					'config_value'		=> array('MTEXT', ''),
				),
				'PRIMARY_KEY'	=> 'config_name',
			));

			$sql = "CREATE TABLE " . $table_prefix . "chat (
				message_id int(11) unsigned NOT NULL auto_increment,
				chat_id int(11) unsigned NOT NULL default '0',
				channel_id int(11) unsigned NOT NULL default '0',
				user_id int(11) unsigned NOT NULL default '0',
				username varchar(255) NOT NULL default '',
				user_colour varchar(6) NOT NULL default '',
				message text NOT NULL,
				bbcode_bitfield varchar(255) NOT NULL default '',
				bbcode_uid varchar(8) NOT NULL default '',
				bbcode_options tinyint(1) unsigned NOT NULL default '7',
				time int(11) unsigned NOT NULL default '0',
				PRIMARY KEY  (message_id)
			) CHARACTER SET utf8 COLLATE utf8_bin";
			$db->sql_query($sql);

			$sql = "CREATE TABLE " . $table_prefix . "chat_channels (
				channel_id int(11) unsigned NOT NULL auto_increment,
				channel_name varchar(255) NOT NULL default '',
				channel_password varchar(255) default '',
				last_message_id int(11) unsigned NOT NULL default '0',
				last_message_text text NOT NULL,
				last_poster_id int(11) unsigned NOT NULL default '0',
				last_poster_username varchar(255) NOT NULL default '',
				last_poster_color varchar(6) NOT NULL default '',
				PRIMARY KEY (channel_id)
			) CHARACTER SET utf8 COLLATE utf8_bin";
			$db->sql_query($sql);

			$sql = "CREATE TABLE " . $table_prefix . "chat_sessions (
				user_id mediumint(8) unsigned NOT NULL default '0',
				username varchar(255) NOT NULL default '',
				user_colour varchar(6) NOT NULL default '',
				user_login int(11) unsigned NOT NULL default '0',
				user_firstpost int(11) unsigned NOT NULL default '0',
				user_lastpost int(11) unsigned NOT NULL default '0',
				user_lastupdate int(11) unsigned NOT NULL default '0',
				PRIMARY KEY  (user_id)
			) CHARACTER SET utf8 COLLATE utf8_bin";
			$db->sql_query($sql);

			$sql = "INSERT INTO " . $table_prefix . "chat_config (config_name, config_value) VALUES ('chat_enabled', '0')";
			$db->sql_query($sql);

			$sql = "INSERT INTO " . $table_prefix . "chat_config (config_name, config_value) VALUES ('chat_disabled_message', 'The chat is currently disabled')";
			$db->sql_query($sql);

			$sql = "INSERT INTO " . $table_prefix . "chat_config (config_name, config_value) VALUES ('guest_view_enabled', '0')";
			$db->sql_query($sql);

			$sql = "INSERT INTO " . $table_prefix . "chat_config (config_name, config_value) VALUES ('chat_popup_enabled', '0')";
			$db->sql_query($sql);

			$sql = "INSERT INTO " . $table_prefix . "chat_config (config_name, config_value) VALUES ('update_seconds', '15')";
			$db->sql_query($sql);

			$sql = "INSERT INTO " . $table_prefix . "chat_config (config_name, config_value) VALUES ('num_message_display', '25')";
			$db->sql_query($sql);

			$sql = "INSERT INTO " . $table_prefix . "chat_config (config_name, config_value) VALUES ('archive_message_limit', '100')";
			$db->sql_query($sql);

			$sql = "INSERT INTO " . $table_prefix . "chat_config (config_name, config_value) VALUES ('permissions_enabled', '0')";
			$db->sql_query($sql);

			$sql = "INSERT INTO " . $table_prefix . "chat_config (config_name, config_value) VALUES ('online_time', '0')";
			$db->sql_query($sql);

			$sql = "INSERT INTO " . $table_prefix . "chat_config (config_name, config_value) VALUES ('idle_time', '300')";
			$db->sql_query($sql);

			$sql = "INSERT INTO " . $table_prefix . "chat_config (config_name, config_value) VALUES ('offline_time', '1800')";
			$db->sql_query($sql);

			$sql = "INSERT INTO " . $table_prefix . "chat_config (config_name, config_value) VALUES ('online_delay', '5')";
			$db->sql_query($sql);

			$sql = "INSERT INTO " . $table_prefix . "chat_config (config_name, config_value) VALUES ('idle_delay', '60')";
			$db->sql_query($sql);

			$sql = "INSERT INTO " . $table_prefix . "chat_config (config_name, config_value) VALUES ('offline_delay', '300')";
			$db->sql_query($sql);

			$umil->table_column_add('phpbb_users', 'user_chat_banned', array('VCHAR', ''));

		case '0.0.2':
			$sql = "INSERT INTO " . $table_prefix . "chat_config (config_name, config_value) VALUES ('channels_enabled', '0')";
			$db->sql_query($sql);

		break;
	}

	$umil->config_update($mod['config'], $mod['version']);

	// Our final action, we purge the board cache
	$umil->cache_purge();

	// We are done
	trigger_error('DONE');
}
else
{
	confirm_box(false, 'INSTALL_CHAT_MOD');
}

// Shouldn't get here.
redirect($phpbb_root_path . $user->page['page_name']);

?>